package com.ruoyi.project.dict.dailyword.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.dailyword.domain.Dailyword;
import com.ruoyi.project.dict.dailyword.service.IDailywordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 每日一字Controller
 *
 * @author shixiangwen
 * @date 2020-04-01
 */
@Controller
@RequestMapping("/dict/dailyword")
public class DailywordController extends BaseController {
    private String prefix = "dict/dailyword";

    @Autowired
    private IDailywordService dailywordService;

    @RequiresPermissions("dict:dailyword:view")
    @GetMapping()
    public String dailyword() {
        return prefix + "/dailyword";
    }

    /**
     * 查询每日一字列表
     */
    @RequiresPermissions("dict:dailyword:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Dailyword dailyword) {
        startPage();
        List<Dailyword> list = dailywordService.selectDailywordList(dailyword);
        return getDataTable(list);
    }

    /**
     * 导出每日一字列表
     */
    @RequiresPermissions("dict:dailyword:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Dailyword dailyword) {
        List<Dailyword> list = dailywordService.selectDailywordList(dailyword);
        ExcelUtil<Dailyword> util = new ExcelUtil<Dailyword>(Dailyword.class);
        return util.exportExcel(list, "dailyword");
    }

    /**
     * 新增每日一字
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存每日一字
     */
    @RequiresPermissions("dict:dailyword:add")
    @Log(title = "每日一字", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Dailyword dailyword) {
        return toAjax(dailywordService.insertDailyword(dailyword));
    }

    /**
     * 修改每日一字
     */
    @GetMapping("/edit/{wid}")
    public String edit(@PathVariable("wid") String wid, ModelMap mmap) {
        Dailyword dailyword = dailywordService.selectDailywordById(wid);
        mmap.put("dailyword", dailyword);
        return prefix + "/edit";
    }

    /**
     * 修改保存每日一字
     */
    @RequiresPermissions("dict:dailyword:edit")
    @Log(title = "每日一字", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Dailyword dailyword) {
        return toAjax(dailywordService.updateDailyword(dailyword));
    }

    /**
     * 删除每日一字
     */
    @RequiresPermissions("dict:dailyword:remove")
    @Log(title = "每日一字", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dailywordService.deleteDailywordByIds(ids));
    }
}
